with Rational_Numbers.Reduce;
with Pgcd;

function Rational_Numbers.Reduce(X:Rational) return Rational is
   Res : Rational;
   Pg : Natural;
begin
   if X.Numerator = 0 then
      Res.Numerator := 0;
      Res.Denominator :=1;
      return Res;
   else if X.Numerator = X.Denominator then
      Res.Numerator := 1;
      Res.Denominator := 1;
      return Res;
   else if X.Numerator < 0 then
      Pg := Pgcd(Natural(-1*X.Numerator), Natural(X.Denominator));
      Res.Numerator := X.Numerator / Integer(Pg);
      Res.Denominator := X.Denominator / Positive(Pg);
      return Res;
   else
      Pg := Pgcd(Natural(X.Numerator), Natural(X.Denominator));
      Res.Numerator := X.Numerator / Integer(Pg);
      Res.Denominator := X.Denominator / Positive(Pg);
      return Res;
   end if;
   end if;
   end if;
end Rational_Numbers.Reduce;
